package com.itheima.day16.work;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Demo2 {
    public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException, ClassNotFoundException {
        //创建类加载器
        ClassLoader classLoader = Demo2.class.getClassLoader();
        //获取流
        InputStream resourceAsStream = classLoader.getResourceAsStream("pro.properties");
        // 创建properties ，从流种获取数据
        Properties  p=new Properties();
        //加载流
        p.load(resourceAsStream);
        resourceAsStream.close();
        //获取了配置文件的键对应的值
        String aClass = p.getProperty("class");
        System.out.println(aClass);
        //通过反射获取到aClass对应的大写的Class对象
      //  Class<?> aClass1 = aClass.getClass(); //此时的aClass只是一个字符串
       Class<?> aClass2 = Class.forName(aClass);
        //获取内存的空参构造
        Constructor declaredConstructor = aClass2.getDeclaredConstructor();
        System.out.println(declaredConstructor);
        //可以访问私有权限
        declaredConstructor.setAccessible(true);
        //获取对象（内存中）
        Object o = declaredConstructor.newInstance();

        Method method = aClass2.getDeclaredMethod(p.getProperty("method"));
        method.setAccessible(true);
        Object invoke = method.invoke(o);
        System.out.println(invoke);

        Field filed1 = aClass2.getDeclaredField(p.getProperty("filed"));
        filed1.setAccessible(true);
        filed1.set(o,"daqiang");
        Object o1 = filed1.get(o);
        System.out.println(o1);
        System.out.println(o.toString());




    }
}
